<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-item label="盘点单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['inventoryOrder']" placeholder="请输入盘点单号" ></a-input>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="24" >
            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['status']" placeholder="请输入状态" style="width: 100%" />
            </a-form-item>
          </a-col> -->
          <a-col :span="24" >
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['remark']" placeholder="请输入备注" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="生效日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择生效日期" v-decorator="['effectiveDate']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="盘点货架" :key="refKeys[0]" :forceRender="true">
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedShelfKeys.length }}</a>项
          <a style="margin-left: 24px" @click="onClearShelves">清空</a>
        </div>
        <a-dropdown v-if="selectedShelfKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item key="1" @click="addShelf"><a-icon type="plus"/>添加</a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
        </a-dropdown>
        <a-table
          :row-selection="{ selectedRowKeys: selectedShelfKeys, onChange: onShelfChange }"
          :columns="shelfColumns"
          :data-source="shelfData"
        />
        <j-editable-table
          :ref="refKeys[0]"
          :loading="checkInventoryShelfTable.loading"
          :columns="checkInventoryShelfTable.columns"
          :dataSource="checkInventoryShelfTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="false"/>
      </a-tab-pane>
      <a-tab-pane tab="盘点品番" :key="refKeys[1]" :forceRender="true">
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedSkuKeys.length }}</a>项
          <a style="margin-left: 24px" @click="onClearSku">清空</a>
        </div>
        <a-dropdown v-if="selectedSkuKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item key="1" @click="addSku"><a-icon type="plus"/>添加</a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
        </a-dropdown>
        <a-table
          :row-selection="{ selectedRowKeys: selectedSkuKeys, onChange: onSkuChange }"
          :columns="skuColumns"
          :data-source="skuData"
        />
        <j-editable-table
          :ref="refKeys[1]"
          :loading="checkInventorySkuTable.loading"
          :columns="checkInventorySkuTable.columns"
          :dataSource="checkInventorySkuTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="false"/>
      </a-tab-pane>
      <a-tab-pane tab="盘点结果" :key="refKeys[2]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[2]"
          :loading="checkInventoryResultTable.loading"
          :columns="checkInventoryResultTable.columns"
          :dataSource="checkInventoryResultTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="false"/>
      </a-tab-pane>
      <a-tab-pane tab="盘点差异" :key="refKeys[3]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[3]"
          :loading="checkInventoryDifferenceTable.loading"
          :columns="checkInventoryDifferenceTable.columns"
          :dataSource="checkInventoryDifferenceTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="false"/>
      </a-tab-pane>
    </a-tabs>
    <a-row v-if="showFlowSubmitButton" style="text-align: center;width: 100%;margin-top: 16px;"><a-button @click="handleOk">提 交</a-button></a-row>
  </a-spin>
</template>

<script>

  import pick from 'lodash.pick'
  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'CheckInventoryForm',
    mixins: [JEditableTableMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 0,
        validatorRules: {
        },
        refKeys: ['checkInventoryShelf', 'checkInventorySku', 'checkInventoryResult', 'checkInventoryDifference', ],
        tableKeys:['checkInventoryShelf', 'checkInventorySku', 'checkInventoryResult', 'checkInventoryDifference', ],
        activeKey: 'checkInventoryShelf',
        // 盘点货架
        checkInventoryShelfTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '货架编码',
              key: 'shelfCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '货架名称',
              key: 'shelfName',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '货架类型编码',
              key: 'shelfTypeCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
          ]
        },
        // 盘点品番
        checkInventorySkuTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '品番',
              key: 'skuCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '客户',
              key: 'customerCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
          ]
        },
        // 盘点结果
        checkInventoryResultTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '客户编号',
              key: 'customerCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '品番',
              key: 'skuCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '品番名称',
              key: 'skuName',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '货格',
              key: 'compartmentCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '条码',
              key: 'labelNumber',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '批次号',
              key: 'batchNumber',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '背番',
              key: 'backNo',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '数量',
              key: 'number',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
          ]
        },
        // 盘点差异
        checkInventoryDifferenceTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '品番',
              key: 'skuCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '品番名称',
              key: 'skuName',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '客户编号',
              key: 'customerCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '货格',
              key: 'compartmentCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '批次号',
              key: 'batchNumber',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '背番',
              key: 'backNo',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '包装数量',
              key: 'packagingNumber',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '预计数量',
              key: 'expectNumber',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '完成数量',
              key: 'finishNumber',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '盘点日期',
              key: 'checkDate',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
          ]
        },
        shelfColumns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'货架编码',
            align:"center",
            dataIndex: 'shelfCode'
          },
          {
            title:'货架名称',
            align:"center",
            dataIndex: 'shelfName'
          },
          {
            title:'货架类型编码',
            align:"center",
            dataIndex: 'shelfTypeCode'
          },
        ],
        skuColumns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'客户编码',
            align:"center",
            dataIndex: 'customerCode'
          },
          {
            title:'客户名称',
            align:"center",
            dataIndex: 'customerName'
          },
          {
            title:'品番编码',
            align:"center",
            dataIndex: 'skuCode'
          },
          {
            title:'品番名称',
            align:"center",
            dataIndex: 'skuName'
          },
          {
            title:'背番',
            align:"center",
            dataIndex: 'backNo'
          },
        ],
        url: {
          shelfList: "/basicShelf/basicShelf/list",
          skuList: "/basicSkuInfo/basicSkuInfo/list",
          add: "/checkInventory/checkInventory/add",
          edit: "/checkInventory/checkInventory/edit",
          queryById: "/checkInventory/checkInventory/queryById",
          checkInventoryShelf: {
            list: '/checkInventory/checkInventory/queryCheckInventoryShelfByMainId'
          },
          checkInventorySku: {
            list: '/checkInventory/checkInventory/queryCheckInventorySkuByMainId'
          },
          checkInventoryResult: {
            list: '/checkInventory/checkInventory/queryCheckInventoryResultByMainId'
          },
          checkInventoryDifference: {
            list: '/checkInventory/checkInventory/queryCheckInventoryDifferenceByMainId'
          },
        },
        /* table选中keys*/
        selectedShelfKeys: [],
        selectionShelves: [],
        selectedSkuKeys: [],
        selectionSku: [],
        shelfData: [],
        skuData: [],
      }
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
      this.loadShelf();
      this.loadSku();
    },
    methods: {
      addShelf(){
        for (var a = 0; a < this.selectionShelves.length; a++) {
          this.checkInventoryShelfTable.dataSource.push(this.selectionShelves[a]);
        }
        this.selectedShelfKeys = [];
        this.selectionShelves = [];
      },
      addSku(){
        for (var a = 0; a < this.selectionSku.length; a++) {
          this.checkInventorySkuTable.dataSource.push(this.selectionSku[a]);
        }
        this.selectedSkuKeys = [];
        this.selectionSku = [];
      },
      addBefore(){
        this.form.resetFields()
        this.checkInventoryShelfTable.dataSource=[]
        this.checkInventorySkuTable.dataSource=[]
        this.checkInventoryResultTable.dataSource=[]
        this.checkInventoryDifferenceTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'createBy','createTime','inventoryOrder','status','remark','effectiveDate')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.checkInventoryShelf.list, params, this.checkInventoryShelfTable)
          this.requestSubTableData(this.url.checkInventorySku.list, params, this.checkInventorySkuTable)
          this.requestSubTableData(this.url.checkInventoryResult.list, params, this.checkInventoryResultTable)
          this.requestSubTableData(this.url.checkInventoryDifference.list, params, this.checkInventoryDifferenceTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          checkInventoryShelfList: allValues.tablesValue[0].values,
          checkInventorySkuList: allValues.tablesValue[1].values,
          checkInventoryResultList: allValues.tablesValue[2].values,
          checkInventoryDifferenceList: allValues.tablesValue[3].values,
        }
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          })
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createBy','createTime','inventoryOrder','status','remark','effectiveDate'))
      },
      onClearShelves() {
        this.selectedShelfKeys = [];
        this.selectionShelves = [];
      },
      onClearSku() {
        this.selectedSkuKeys = [];
        this.selectionSku = [];
      },
      onShelfChange(selectedRowKeys, selectionRows) {
        console.log('selectedRowKeys changed: ', selectedRowKeys);
        console.log('selectionRows changed: ', selectionRows);
        this.selectedShelfKeys = selectedRowKeys;
        this.selectionShelves = selectionRows;
      },
      onSkuChange(selectedRowKeys, selectionRows) {
        console.log('selectedRowKeys changed: ', selectedRowKeys);
        console.log('selectionRows changed: ', selectionRows);
        this.selectedSkuKeys = selectedRowKeys;
        this.selectionSku = selectionRows;
      },
      loadShelf(arg) {
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        this.loading = true;
        getAction(this.url.shelfList).then((res) => {
          if (res.success) {
            console.log(res.result);
            this.shelfData = res.result.records||res.result;
          }
          if(res.code===510){
            this.$message.warning(res.message)
          }
          this.loading = false;
        })
      },
      loadSku(arg) {
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        this.loading = true;
        getAction(this.url.skuList).then((res) => {
          if (res.success) {
            console.log(res.result);
            this.skuData = res.result.records||res.result;
          }
          if(res.code===510){
            this.$message.warning(res.message)
          }
          this.loading = false;
        })
      },
    }
  }
</script>

<style scoped>
</style>